<?php

if ( ! function_exists('get_month_name'))
{
	// date format : 
	function is_date_after($date, $after_date)
	{
		return ( strtotime($date) < strtotime($after_date) );
	}
}

if ( ! function_exists('get_month_name'))
{
	function get_month_name($idx, $lang = LANG_ENGLISH, $short = false)
	{
		$month=get_array_month($lang, $short);//array("January","February","March","April","May","June","July","August","September","October","November","December");
		return $month[intval($idx)-1];
	}
}

if ( ! function_exists('get_month'))
{
	function get_month($date)
	{
		return substr($date,5,2);
	}
}

if ( ! function_exists('to_start'))
{
	function to_start($date)
	{
		return substr($date,0,count($from)-4)."-01";
	}
}


if ( ! function_exists('to_end'))
{
	function to_end($date)
	{
		$md=substr($date,5,2);
		$yd=substr($date,0,4);
		$cd=days_in_month($md, $yd);
		return substr($date,0,count($date)-4)."-".$cd;
	}
}

if ( ! function_exists('to_idformat'))
{
	function to_idformat($date)
	{
		$dd=substr($date,8,10);
		$md=substr($date,5,2);
		$md=substr(get_month_name($md),0,3);
		$yd=substr($date,0,4);
		return "$dd $md $yd";
	}
}

if ( ! function_exists('to_idformat_timestamp'))
{
	function to_idformat_timestamp($date_string, $short_month = FALSE, $date_only = FALSE) {
		$time = strtotime($date_string);

		if($short_month) {
			$month = 'M';
		} else {
			$month = 'F';
		}

		if($date_only) {
			return date("d {$month} Y", $time);
		} else {
			return date("d {$month} Y | H.i", $time);
		}
	}
}

if ( ! function_exists('to_idformat_timestamp_no_time'))
{
	function to_idformat_timestamp_no_time($date_string, $short_month = FALSE, $date_only = TRUE) {
		$time = strtotime($date_string);

		if($short_month) {
			$month = 'M';
		} else {
			$month = 'F';
		}

		if($date_only) {
			return date("d {$month} Y", $time);
		} else {
			return date("d {$month} Y | H.i", $time);
		}
	}
}

if ( ! function_exists('plus_day'))
{
	function plus_day($date,$idx)
	{
		$dd=substr($date,8,10);
		$md=substr($date,5,2);
		$yd=substr($date,0,4);
		$m=days_in_month($md);
		$dd=$dd+$idx;
		if($m<$dd) {
			$dd="01";
			$md++;
			if($md>12){
				$md="01";
				$yd++;
			}
		}
		return "$yd-$md-$dd";
	}
}
if ( ! function_exists('minus_day'))
{
	function minus_day($date,$idx)
	{
		$dd=substr($date,8,10);
		$md=substr($date,5,2);
		$yd=substr($date,0,4);
		$m=days_in_month($md-1);
		$dd=$dd-$idx;
		if($dd<1) {
			$dd=$m;
			$md--;
			if($md<1){
				$md="12";
				$yd--;
			}
		}
		return "$yd-$md-$dd";
	}
}
if ( ! function_exists('plus_month'))
{
	function plus_month($date,$idx)
	{
		$dd=substr($date,8,10);
		$md=substr($date,5,2);
		$yd=substr($date,0,4);
		$md=$md+$idx;
		$m=days_in_month($md);
		if($m<$dd) {
			$dd="01";
			if($md>12){
				$md="01";
				$yd++;
			}
		}
		return "$yd-$md-$dd";
	}
}
if ( ! function_exists('minus_month'))
{
	function minus_month($date,$idx)
	{
		$dd=substr($date,8,10);
		$md=substr($date,5,2);
		$yd=substr($date,0,4);
		$md=$md-$idx;
		$m=days_in_month($md);
		if($dd<1) {
			$dd=$m;
			if($md<1){
				$md="12";
				$yd--;
			}
		}
		return "$yd-$md-$dd";
	}
}

if ( ! function_exists('get_date_between'))
{
	function get_date_between($from, $to)
	{
		$dateMonthYearArr = array();
		$fromDateTS = strtotime($from);
		$toDateTS = strtotime($to);

		for ($currentDateTS = $fromDateTS; $currentDateTS <= $toDateTS; $currentDateTS += (60 * 60 * 24)) {
			// use date() and $currentDateTS to format the dates in between
			$currentDateStr = date("Y-m-d",$currentDateTS);
			$dateMonthYearArr[] = $currentDateStr;
		}

		return $dateMonthYearArr;
	}
}

if ( ! function_exists('get_week_between'))
{
	function get_week_between($from, $to)
	{

		$dateMonthYearArr = array();
		$fromDateTS = strtotime($from);
		$toDateTS = strtotime($to);

		for ($currentDateTS = $fromDateTS; $currentDateTS <= $toDateTS; $currentDateTS += (60 * 60 * 24)) {
			// use date() and $currentDateTS to format the dates in between
			$currentDateStr = date("Y-m-d",$currentDateTS);
			$dateMonthYearArr[] = $currentDateStr;
		}

		return $dateMonthYearArr;
	}
}

if ( ! function_exists('get_array_month'))
{
	function get_array_month($format = LANG_ENGLISH, $short = true)
	{
		$arrMonth = array();
		if ($format == LANG_ENGLISH ){
			$arrMonth = array("January","February","March","April","May","June","July","August","September","October","November","December");
		}else if ($format == LANG_INDONESIA ){
			$arrMonth = array("Januari","Februari","Maret","April","Mei","Juni","Juli","Agustus","September","Oktober","November","Desember");
		}
		return $arrMonth;
	}
}
